package ihm;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.Remote;
import java.rmi.RemoteException;
import java.util.Scanner;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;

import rmi.InvokeRMI;




public class Acceuil extends JFrame{

	private JPanel container = new JPanel();
	//Tableau contenant les applications disponible
	String[] tab_string ;
	JButton[] tab_button;
	private JLabel ecran = new JLabel();
	private Dimension dim = new Dimension(200, 40);
	private Dimension dim2 = new Dimension(50, 31);
	private Remote r;

	public void lauch() {

		this.setSize(240, 500);
		this.setTitle("Tom4");
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		this.setLocationRelativeTo(null);
		this.setResizable(false);
		//On initialise le conteneur avec tous les composants
		initComposant();
		//On ajoute le conteneur
		this.setContentPane(container);
		this.setVisible(true);


	}

	private void initComposant() {
		System.out.println("Lancement du client");

		//On d�finit la police d'�criture � utiliser
		Font police = new Font("Arial", Font.BOLD, 20);

		//On aligne les informations � droite dans le JLabel
		ecran.setHorizontalAlignment(JLabel.RIGHT);
		ecran.setPreferredSize(new Dimension(220, 20));
		JPanel operateur = new JPanel();      
		operateur.setPreferredSize(new Dimension(55, 225));
		JPanel chiffre = new JPanel();
		chiffre.setPreferredSize(new Dimension(165, 225));
		JPanel panEcran = new JPanel();
		panEcran.setPreferredSize(new Dimension(220, 30));
		Object o [] = { new Integer (5)};
		//Cr�ation du registery
		try {
			//Avec registery
			 r = Naming.lookup("rmi://" + InetAddress.getLocalHost().getHostAddress() + "/Invoker");
			System.out.println("Cr�ation du registery : "+"rmi://" + InetAddress.getLocalHost().getHostAddress() + "/Invoker");
			//Sans registery
			//r=getComputeServer();
			if (r instanceof InvokeRMI) {
				Object s = ((InvokeRMI) r).Invoke("classe.Calculatrice", "Addition", o);
				System.out.println("chaine renvoyee = " + s);
				//R�cup�ration de la liste des applications
				tab_string=((InvokeRMI) r).getApplications();
				tab_button=new JButton[tab_string.length];
				for(int i = 0; i < tab_string.length; i++){
					String l = (String) tab_string[i].subSequence( tab_string[i].length()-3,tab_string[i].length());
					System.out.println(l);
					if (l=="bin") {
						//(String) tab_string[i].subSequence( 0,tab_string[i].length()-4)
						tab_button[i] = new JButton("l");
						tab_button[i].setPreferredSize(dim);
						tab_button[i].addActionListener(new action());
						chiffre.add(tab_button[i]);
					}
					tab_button[i] = new JButton( tab_string[i]);
					tab_button[i].setPreferredSize(dim);
					tab_button[i].addActionListener(new action());
					chiffre.add(tab_button[i]);


				}

			}
			else {
				System.out.println("RMI non trouv�");
			}
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (RemoteException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotBoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		panEcran.add(ecran);
		panEcran.setBorder(BorderFactory.createLineBorder(Color.black));
		container.add(panEcran, BorderLayout.NORTH);
		container.add(chiffre, BorderLayout.CENTER);
		container.add(operateur, BorderLayout.EAST);

	}

	class action implements ActionListener {
		public void actionPerformed(ActionEvent arg0){
			System.out.println(arg0.getActionCommand().subSequence(0, arg0.getActionCommand().length()-4));
			String classe =(String) arg0.getActionCommand().subSequence(0, arg0.getActionCommand().length()-4);
			try {
				System.out.println("Methode :");
				Scanner lecteur = new Scanner(System.in);
				String methode =lecteur.nextLine();
				System.out.println("Argument :");
				String argument =lecteur.nextLine();

				Object o [] = { new Integer (Integer.parseInt(argument))};
				Object s = ((InvokeRMI) r).Invoke("classe."+classe, methode, o);
				System.out.println("chaine renvoyee = " + s);
			} catch (RemoteException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}

		
	}



	private InvokeRMI getComputeServer() throws NotBoundException, FileNotFoundException, IOException, ClassNotFoundException {
		//CHANGED: The 'normal' way using the registry:
		//Registry registry = LocateRegistry.getRegistry();
		//Compute compute = (Compute)registry.lookup(Compute.SERVICE_NAME);
		//return compute;

		//Reading from a file instead: 
		Socket comm = new Socket("localhost", 10080);
		 OutputStream os = comm.getOutputStream();
		    ObjectOutputStream ecrivain  = new ObjectOutputStream(os);
		InputStream is = comm.getInputStream();
	    ObjectInputStream lecteur = new ObjectInputStream (is);
	    ecrivain.writeInt(1);
	    
		try {
			return (InvokeRMI) lecteur.readObject();
		} finally  {
			lecteur.close();
		}
	}

}
